package LeetCode.month01.day22;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class No47 {
    static boolean[] visit;
    public static void main(String[] args) {
        System.out.println(permuteUnique(new int[]{1,1,2}));
    }
    public static List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        Arrays.sort(nums);
        int len = nums.length;
        visit = new boolean[len];
        deepseek(result,list,0,nums);
        return result;
    }

    public static void deepseek(List<List<Integer>> result,List<Integer> list,int index,int[] nums){
        if(index==nums.length){
            result.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(visit[i]||i > 0 && nums[i] == nums[i - 1]&&!visit[i-1]) continue;
            list.add(nums[i]);
            visit[i] = true;
            deepseek(result,list,index+1,nums);
            visit[i]=false;
            list.remove(index);
        }
    }


}
